package se.webbzon.boltzmann.game.object.item;

import java.nio.ByteBuffer;

import se.webbzon.boltzmann.environment.AbstractEnvironment;
import se.webbzon.boltzmann.item.AbstractItem;
import se.webbzon.boltzmann.item.ItemGroup;
import se.webbzon.oschi01.friendly.Friendly;
import se.webbzon.oschi01.worldeditor.WorldStaticObjectDeflation;

public class GameItemDeflation extends WorldStaticObjectDeflation {
	
	private long id;
	
	private AbstractItem item;

	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new empty game item deflation. **/
	public GameItemDeflation(int deflationProperties) {
		super(deflationProperties, null);
	}
	
	/** Creates a new game item deflation of an existing game item. **/
	public GameItemDeflation(int deflationProperties, GameItem item) {
		super(deflationProperties, null, item);
		id = item.getId();
		this.item = item.getItem();
	}

	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public void inflate(ByteBuffer buff) {
		super.inflate(buff);
		id = buff.getLong();
		AbstractItem it = ItemGroup.instance().getItem(buff.getInt());
		if (it == null)
			item = null;
		else
			item = (AbstractItem) it.clone();
	}

	@Override public ByteBuffer deflate(int allocate) {
		ByteBuffer buff = super.deflate(allocate + 12);
		buff.putLong(id);
		buff.putInt(ItemGroup.instance().getId(item));
		return buff;
	}
	
	/*============================================================
	Friendly Methods
	============================================================*/
	
	/** Creates a new game item of this deflation. **/
	@Friendly GameItem makeGameItem(AbstractEnvironment env) {
		GameItem gameItem = new GameItem(env,id,item);
		gameItem.setPosition(getX(), getY(), getZ());
		gameItem.setSize(getWidth(), getHeight(), getDepth());
		gameItem.setRotation(getEX(), getEY(), getEZ(), getAngle());
		return gameItem;
	}

}
